local CanRecastCard = fk.CreateSkill {
  name = "#CanRecastCard",
}
---@class Utility_LB_Kill
local LB_U = require "packages.lb_kill_utility._base"

Fk:loadTranslationTable {
  ["#CanRecastCard"] = "重铸",
  ["#CanRecastCard-ask"] = "你可以重铸一张牌",
}

CanRecastCard:addEffect(fk.DamageFinished, {
  mute = true,
  priority = 0.001,
  can_trigger = function (self, event, target, player, data)
    return not data.prevented and player:isAlive() and (player == data.to or player == data.from ) and table.find(player:getCardIds("he"), function (id)
      local card = Fk:getCardById(id)
      for name, value in pairs(card.mark) do
        if name:find("-can_recast", 1, true) then
          return true
        end
      end
    end)
  end,
  on_cost = function (self, event, target, player, data)
    local cards = table.filter(player:getCardIds("he"), function (id)
      local card = Fk:getCardById(id)
      for name, value in pairs(card.mark) do
        if name:find("-can_recast", 1, true) then
          return true
        end
      end
    end)
    if #cards == 0 then return end
    cards = player.room:askToCards(player, {
      min_num = 1,
      max_num = 1,
      skill_name = "recast",
      cancelable = true,
      pattern = tostring(Exppattern {id = cards}),
      prompt = "#CanRecastCard-ask",
    })
    if #cards > 0 then
      event:setCostData(self, {cards = cards})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    player.room:recastCard(event:getCostData(self).cards, player)
  end,
})

return CanRecastCard
